﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using QuanLyBus.Model;

namespace QuanLyBus
{
    public partial class Them_tram_thuoc_tuyen : PhoneApplicationPage
    {
        QL_TuyenDataContext db = new QL_TuyenDataContext(App.constr);
        List<ds_tuyen> tuyen = new List<ds_tuyen>();
        List<ds_tram> tram = new List<ds_tram>();
        private int tuyen_lpk,tram_lpk;
        //string[] tuyen = { "Chợ Lớn - Bến Thành","Bến Thành - Chợ Lớn"};
        public Them_tram_thuoc_tuyen()
        {
            InitializeComponent();
            tuyen=db.ds_tuyens.ToList();
            tram = db.ds_trams.ToList();            
            this.lpktuyen.ItemsSource = tuyen;
            this.lpktram.ItemsSource = tram;
        }
        private byte xet_luot()
        {
            //a=true luot di;
            byte a=0;
            if(rd_luot_di.IsChecked==true)
            {
                a = 1;
            }
            return a;
        }
        private void lpktuyen_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                ds_tuyen tuyen = lpktuyen.SelectedItem as ds_tuyen;
                tuyen_lpk=tuyen.id_tuyen;
            }
            catch
            {
                lpktuyen.SelectedIndex=-1;               
            }
            
        }

        private void btn_add_Click(object sender, EventArgs e)
        {            
            byte luot=xet_luot();
            int thu_tu_txt = int.Parse(txt_thu_tu_tram.Text);
            db.tram_thuoc_tuyens.InsertOnSubmit(new tram_thuoc_tuyen() {id_tram=tram_lpk,id_tuyen=tuyen_lpk,thu_tu_tram_dung=thu_tu_txt,tram_ve=luot });
            db.SubmitChanges();
            MessageBox.Show("Thêm thành công");
            this.NavigationService.Navigate(new Uri("/tram.xaml", UriKind.Relative));

        }
        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            this.NavigationService.GoBack();
            base.OnBackKeyPress(e);
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.NavigationService.GoBack();
        }

        private void lpktram_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ds_tram tram = lpktram.SelectedItem as ds_tram;
            tram_lpk = tram.id_tram;
        }

        private void rd_luot_di_Checked(object sender, RoutedEventArgs e)
        {

        }
    }
}